home *** CD-ROM | disk | FTP | other *** search
- /*
- Thermometer XCMD v1.0
-
- ©1991 Apple Computer, Inc.; by Mike Byrne
-
- This XCMD can be used to make little "Thermometer"-like progress bars. It should be passed the
- rect in which the bar should be painted, the current value of some counter, the maximum value
- of that counter, and, optionally, a color. The colors are the basic quickDraw colors, and the
- default is black. It will check to see if the rect is longer or tall. If it is long, it fills
- left to right. If it is tall, it fills top to bottom.
-
- Form:
- Thermometer <rect>, <current>, <maximum>, [<color>]
-
- # the MPW 3.2 build commands:
- C -b Thermometer.c -mbg off
- Link -w -t STAK -c WILD -rt XCMD=617 ∂
- -m ENTRYPOINT ∂
- -sg Thermometer ∂
- Thermometer.c.o ∂
- "{Libraries}HyperXLib.o" ∂
- "{Libraries}Runtime.o" ∂
- "{Libraries}Interface.o" ∂
- "{CLibraries}StdCLib.o" ∂
- -o "teststack"
- */
-
- #include <Types.h>
- #include <Windows.h>
- #include <string.h>
- #include <Memory.h>
- #include <QuickDraw.h>
- #include "HyperXCmd.h"
-
- #define NULL '\0'
- #define NIL 0L
- #define FALSE 0
- #define TRUE 1
-
- #define kNumParams 3
-
-
- /* prototypes */
- void ErrorBack(XCmdPtr paramPtr, char *message);
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
- void UnlockParams ( XCmdPtr paramPtr, short paramCount );
- short SwitchColor( char* ask );
- Boolean StringRect(char* theString, Rect* theRect);
-
-
- pascal void EntryPoint(XCmdPtr paramPtr)
- {
- /* variable declarations */
- char rectStr[20];
- char curStr[10];
- char maxStr[10];
- long curVal;
- long maxVal;
- Rect theRect;
- short theColor;
- short width;
- short height;
- WindowPtr theWindow;
-
-
- /* move high and lock the parameters. */
- MoveLockParams(paramPtr, paramPtr->paramCount);
-
- /* check for copyright or syntax help request */
- if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
- ErrorBack(paramPtr, "v1.0, ©1991 Apple Computer, Inc.; by Mike Byrne");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
- ErrorBack(paramPtr, "Thermometer syntax is 'Thermometer <rect>, <current>, <maximum>, [<color>], [<pattern>]'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* not a copyright or help request. */
- /* check for correct number of parameters */
- if (paramPtr->paramCount < kNumParams) {
- ErrorBack(paramPtr, "Error: Thermometer syntax is 'Thermometer <rect>, <current>, <maximum>, [<color>], [<pattern>]'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- GetPort(&theWindow);
- SetPort(theWindow);
-
- /* set up the basic parameters and such. */
- strcpy(rectStr, (char*)*paramPtr->params[0]);
- strcpy(curStr, (char*)*paramPtr->params[1]);
- strcpy(maxStr, (char*)*paramPtr->params[2]);
- c2pstr(curStr);
- c2pstr(maxStr);
- if (!StringRect(rectStr, &theRect)) {
- ErrorBack(paramPtr, "Error: One of the 'rect' values out-of-range.");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
- StringToNum(curStr, &curVal);
- StringToNum(maxStr, &maxVal);
- if (curVal < 0) { curVal = 0; }
- if (maxVal < 1) { maxVal = 1; }
- if (curVal > maxVal) { curVal = maxVal; }
-
- /* set the color */
- if ( (paramPtr->paramCount < 4) || !(theColor = SwitchColor((char*)*paramPtr->params[3])) ) {
- theColor = blackColor;
- }
- ForeColor(theColor);
-
- /* specify the rect */
- InsetRect(&theRect,1,1);
- width = theRect.right - theRect.left;
- height = theRect.bottom - theRect.top;
- if (width >= height) {
- theRect.right = theRect.left + (width*curVal/maxVal);
- } else {
- theRect.top = theRect.bottom - (height*curVal/maxVal);
- }
- PaintRect(&theRect);
- ForeColor(blackColor);
- }
-
-
-
-
- /* ++++++++++++++++++++++++++++++++++++++++++++++++++++
- SwitchColor takes a string and returns a color... */
- short SwitchColor( char* ask )
- {
- short i;
-
- /* convert the string to uppercase */
- for (i=0; i < strlen(ask); i++) {
- ask[i] = toupper(ask[i]);
- }
-
- /* switch on the string. */
- if (!strcmp(ask, "BLACK") || !strcmp(ask, "1"))
- return(blackColor);
- else if (!strcmp(ask, "WHITE") || !strcmp(ask, "2"))
- return(whiteColor);
- else if (!strcmp(ask, "RED") || !strcmp(ask, "3"))
- return(redColor);
- else if (!strcmp(ask, "GREEN") || !strcmp(ask, "4"))
- return(greenColor);
- else if (!strcmp(ask, "BLUE") || !strcmp(ask, "5"))
- return(blueColor);
- else if (!strcmp(ask, "CYAN") || !strcmp(ask, "6"))
- return(cyanColor);
- else if (!strcmp(ask, "MAGENTA") || !strcmp(ask, "7"))
- return(magentaColor);
- else if (!strcmp(ask, "YELLOW") || !strcmp(ask, "8"))
- return(yellowColor);
- else return(0);
- }
-
-
- /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- StringRect converts a string of the form "l,t,b,r" to a rect. */
- Boolean StringRect(char* theString, Rect* theRect)
- {
- char leftStr[10];
- char topStr[10];
- char rightStr[10];
- char bottomStr[10];
- long left;
- long top;
- long right;
- long bottom;
-
- strcpy(leftStr, strtok(theString,","));
- c2pstr(leftStr);
- StringToNum(leftStr, &left);
- if (left < 0) { return(FALSE); }
-
- strcpy(topStr, strtok(NULL,","));
- c2pstr(topStr);
- StringToNum(topStr, &top);
- if (top < 0) { return(FALSE); }
-
- strcpy(rightStr, strtok(NULL,","));
- c2pstr(rightStr);
- StringToNum(rightStr, &right);
- if (right < 0) { return(FALSE); }
-
- strcpy(bottomStr, strtok(NULL,","));
- c2pstr(bottomStr);
- StringToNum(bottomStr, &bottom);
- if (bottom < 0) { return(FALSE); }
-
- theRect->left = left;
- theRect->top = top;
- theRect->right = right;
- theRect->bottom = bottom;
- return(TRUE);
- }
-
-
-
- /* allocate and load up paramPtr->returnValue with a string
- -------------------------------------------------------- */
- void ErrorBack(XCmdPtr paramPtr, char *message)
- {
- Handle mesHnd;
-
- /*
- Allocate space for an error message.
- Copy the string into it.
- Return the handle to HyperCard.
- */
- mesHnd = NewHandle((long)(strlen(message)+1));
- if (mesHnd == NIL) return;
- strcpy((char *)*mesHnd,message);
- paramPtr->returnValue = mesHnd;
- }
-
-
-
- /* move high and lock down all parameters
- ----------------------------------------------------------------------- */
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
- {
- short i;
-
- for(i=0; i <= paramCount-1; i++)
- {
- MoveHHi(paramPtr->params[i]);
- HLock(paramPtr->params[i]);
- }
- }
-
-
-
-
- /* unlock all parameter handles in the XCmdBlock
- --------------------------------------------- */
- void UnlockParams ( XCmdPtr paramPtr, short paramCount )
- { short i;
-
- for(i=0; i <= paramCount-1; i++)
- { HUnlock(paramPtr->params[i]);}
- }
-